import Vue from 'vue'
import VueRouter from 'vue-router'
// 解决重复点击导航路由报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
  return originalPush.call(this, location).catch(err => {});
}

Vue.use(VueRouter)
//路由重叠项
const routes = [
  {
    path: '/',
    redirect: '/home',
    meta:{
      isTabShow:true
    },
  },
  // 首页路由
  {
    path: '/home',
    name: 'home',
    meta:{
      isTabShow:true,
      num:0
    },
    component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue'),
    children:[
      {
        path:"/home/searchPopup",
        name:"searchPopup",
        component:()=>import(/* */'../views/searchPopup.vue')
      }
    ]
  },
  //专题路由
  {
    path: '/topic',
    name: 'topic',
    meta:{
      isTabShow:true,
      num:1
    },
    component:()=>import(/* webpackChunkName: topic */'../views/topic.vue')
  },
  //分类路由
  {
    path: '/type',
    name: 'type',
    meta:{
      isTabShow:true,
      num:2
    },
    component:()=>import(/* webpackChunkName: type */'../views/type.vue'),
    children:[
      {
        path:"/type/searchPopup",
        name:"searchPopup1",
        component:()=>import(/* */'../views/searchPopup.vue')
      }
    ]
  },
  //购物车路由
  {
    path: '/cart',
    name: 'cart',
    meta:{
      isTabShow:true,
      num:3
    },
    component:()=>import(/* webpackChunkName: cart */'../views/cart.vue')
  },
  //我的路由
  {
    path: '/user',
    name: 'user',
    meta:{
      isTabShow:true,
      num:4
    },
    component:()=>import(/* webpackChunkName: user */'../views/user.vue')
  },
  //首页分类
  {
    path: '/shouyefenlei',
    name: 'shouyefenlei',
    component:()=>import(/* webpackChunkName: shouyefenlei */'../views/shouyefenlei.vue')
  },
  {
    path: '/404',
    name: '404',
    component:()=>import(/* webpackChunkName: 404 */'../views/404.vue')
  },
   //首页 品牌
  {
    path: '/pinp',
    name: 'pinp',
    component:()=>import(/* webpackChunkName: pinp */'../views/pinp.vue')
  },
   //详情页
  {
    path: '/xiangqing',
    name: 'xiangqing',
    component:()=>import(/* webpackChunkName: xiangqing */'../views/xiangqingye.vue')
  },
 

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
/* 
  路由守卫

*/
router.beforeEach((to,from,next)=>{
  // console.log(to);
  ///to 你要去的路由
  //from 你从哪里来
  //next 是一个函数 表示下一步
  if(to.path=="/cart"){
    //判断token
    let token=true;
    if(token){

      //有token证明你登陆过
      next()
    }
    else{
      //没有token  不让你去购物车 强制去/user路由

      next("/user")
    }
  }
  else{
    next()
  }
})
export default router
